package com.jkgj.skykingkong.tms.application;

import com.jkgj.skykingkong.tms.application.command.ConsultationCommand;
import com.jkgj.skykingkong.tms.application.dto.ConsultationInfo;
import com.jkgj.skykingkong.tms.application.query.ConsultationQuery;
import com.jkgj.skykingkong.tms.domain.model.entity.Consultation;
import com.jkgj.skykingkong.tms.domain.model.entity.ConsultationRepository;
import com.jkgj.skykingkong.tms.domain.model.rbac.User;
import com.jkgj.skykingkong.tms.domain.model.rbac.UserRepository;
import com.jkgj.skykingkong.tms.facade.ConsultationListServer;
import com.jkgj.skykingkong.tms.infrastructure.util.mapper.BeanMapper;
import io.ebean.ExpressionList;
import io.ebean.PagedList;
import io.ebean.text.PathProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.ebean.querychannel.EbeanQueryChannelService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @Author: Created by sunjh
 * @Decription:
 * @Date: Created in 11:29  2017/9/21 0021.
 * @Modified:
 */


@Service("consultationListServer")
public class ConsultationService implements ConsultationListServer{
    private static Logger logger = LoggerFactory.getLogger(ConsultationService.class);

    @Autowired
    private ConsultationRepository consultationRepository;
    @Autowired
    private UserRepository userRepository;


    @Transactional
    public void createConsultation(ConsultationCommand consultationCommand){
        logger.info("############# ConsultationService createConsultation() 调用#############");
        Consultation consultation = new Consultation();
        try {
            User server = userRepository.findOne(consultationCommand.getServerUid());
            if(server != null){
                server.setId(consultationCommand.getServerUid());
                consultation.setServer(server);
                consultation.setChannel(consultationCommand.getChannel());
                consultation.setCreateTime(new Date());
                consultation.setStatus(1);//1等待状态
                consultationRepository.save(consultation);
            }
        }catch (Exception ex){
            logger.info("############# ConsultationService createConsultation() userRepository() 用户不存在#############");
            ex.printStackTrace();
        }
    }

    @Transactional
    public void startConsultation( ConsultationCommand consultationCommand){
        logger.info("############# ConsultationService startConsultation() 调用#############");
        Consultation consultation = consultationRepository.findOneByProperty("channel", consultationCommand.getChannel());
        if(consultation != null){
            User client = userRepository.findOne(consultationCommand.getClientUid());
            if(client != null){
                client.setId(consultationCommand.getClientUid());
                consultation.setClient(client);
                consultation.setStartTime(new Date());
                consultation.setStatus(2);//2会诊中
                consultationRepository.update(consultation);
            }
        }
    }
    @Transactional
    public void endConsultation( ConsultationCommand consultationCommand){
        logger.info("############# ConsultationService endConsultation() 调用#############");
        Consultation consultation = consultationRepository.findOneByProperty("channel", consultationCommand.getChannel());
        if(consultation != null){
            consultation.setEndTime(new Date());
            consultation.setStatus(3);//3会诊已结束
            consultationRepository.update(consultation);
        }
    }

    //分页查询会诊
    @Override
    public Page<ConsultationInfo> findConsltationList(Pageable pageable, ConsultationQuery query) {
        ExpressionList<Consultation> expressionList = EbeanQueryChannelService.query(Consultation.class)
                .apply(PathProperties.parse("channel,server(name),client(name),startTime,endTime,status,createTime"))
                .where();
        PagedList<Consultation> pagedList = EbeanQueryChannelService.queryWithPage(expressionList ,
                pageable.getPageNumber() , pageable.getPageSize()).findPagedList();
        return new PageImpl<ConsultationInfo>(BeanMapper.mapList(pagedList.getList(),
                Consultation.class, ConsultationInfo.class),
                pageable , pagedList.getTotalCount());
    }
}
